//
//  PGPostViewModel.m
//  youqu
//
//  Created by 彭鹏 on 16/10/16.
//  Copyright © 2016年 pengpeng. All rights reserved.
//

#import "PGPostViewModel.h"
#import "PGPostItem.h"
#import "PGTopCommentItem.h"
@implementation PGPostViewModel

- (void)setItem:(PGPostItem *)item {
    _item = item;
    
    //top
    CGFloat topY = 0;
    CGFloat topX = 0;
    CGFloat topW = SCREENW;
    CGFloat originY = 65;
    CGFloat textW = SCREENW - 2*commonMargin;
    
//    CGFloat textH = [item.text sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(textW, MAXFLOAT)].height;
    CGFloat textH = [item.text boundingRectWithSize:CGSizeMake(textW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0]} context:nil].size.height;
    CGFloat topH = originY + textH;
    _topViewFrame = CGRectMake(topX, topY, topW, topH);
    _cellH = CGRectGetMaxY(_topViewFrame) + commonMargin;
    
    
    switch (item.type) {
        case PGPostTypeText:
        {
            _cellH = _cellH + 0;
        }
            break;
        case PGPostTypePicture:
        case PGPostTypeVideo:
        case PGPostTypeVoice:
        case PGPostTypeAll:
        {
            CGFloat middleY = _cellH;
            CGFloat middleX = commonMargin;
            CGFloat middleW = SCREENW - 2*commonMargin;
            CGFloat middleH = middleW/item.width*item.height;
            
            if (middleH >= SCREENH) {
                middleH = 250;
                item.is_big = YES;
            }
            
            _middleViewFrame = CGRectMake(middleX, middleY, middleW, middleH);
            _cellH = CGRectGetMaxY(_middleViewFrame) + commonMargin;
        }
            break;
        
    }
    
    
    if (item.topCommentItem) {
        CGFloat originH = 21;
        CGFloat hotCommentY = _cellH;
        CGFloat hotCommentX = commonMargin;
        CGFloat hotCommentW = SCREENW-2*commonMargin;
        CGFloat hotCommentH = 21;
        if (item.topCommentItem.content.length) {
            hotCommentH = [item.topCommentItem.totalContent sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(textW, MAXFLOAT)].height;
        }
        
        _hotCommentViewFrame = CGRectMake(hotCommentX, hotCommentY, hotCommentW, hotCommentH+originH);
        _cellH = CGRectGetMaxY(_hotCommentViewFrame) + commonMargin;
        
    }
    
    CGFloat bottomViewY = _cellH-commonMargin;
    CGFloat bottomViewX = 0;
    CGFloat bottomViewW = SCREENW;
    CGFloat bottomViewH = titleHeight;
    _bottomViewFrame = CGRectMake(bottomViewX, bottomViewY, bottomViewW, bottomViewH);
    _cellH = CGRectGetMaxY(_bottomViewFrame)+commonMargin;
    
    
    
    
}

@end
